using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Roslyn.Compilers.CSharp;
using Roslyn.Compilers.Common;
using Roslyn.Services;

namespace ContentOverContainer.Common
{
    /// <summary>
    /// This rewriter is supposed to process the references of multiplicity nodes to be adjusted.
    /// </summary>
    /// <author>Harald Binkle</author>
    /// 
    /// Hint???: http://stackoverflow.com/questions/9775335/syntaxrewriter-and-multiple-statements
    public class ReferencesRewriter : SyntaxRewriter
    {
        private readonly ReferencesList m_References = new ReferencesList();


        public ISolution Solut { get; set; }

        public ReferencesRewriter(ISolution pSol, ReferencesList pReferences)
        {
            Solut = pSol;
            m_References = pReferences;
        }


        public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
        {
            var kind = node.Expression.Kind;
            var childnodes = node.Expression.ChildNodes();

            //see if this node is in the ReferencesList
            //http://social.msdn.microsoft.com/Forums/vstudio/en-US/eff51118-d091-4b2b-b9b3-8999557d20eb/is-there-a-way-to-compare-syntax-nodes-ignoring-trivia

            var foundChilds =m_References.Where(pair => childnodes.Any(child => Syntax.AreEquivalent(child, (SyntaxNode) pair.Node, false)));
            //IEnumerable<String> intersect = props.Where(p => ids.Any(i => p.Contains(i)));
            var expr = foundChilds.ToList();
            SyntaxNode result;
            if (expr.Count > 0)
            {
                if (kind == SyntaxKind.AssignExpression)
                    result = ExpressionSyntaxConverter.ConvertAssignExpression((BinaryExpressionSyntax) node.Expression);
                else result = base.VisitExpressionStatement(node);
            }
            else
                result =  base.VisitExpressionStatement(node);
            return result;
        }
    }
}
